<template>
	<div style="display: flex;justify-content: space-between;">
		<h3>学生显示</h3>
		<h3>
			<router-link to="/adm/stu/show"><button>返回</button></router-link>
		</h3>
		<h3>
			<button @click="go2Add()">继续添加</button>
		</h3>
	</div>
	<hr>
	<div style="width: 80%; border: 1px solid blue;">
		学生编号:<input type="text" v-model="stu.uid">
		<hr>
		学生姓名:<input type="text" v-model="stu.uname">
		<hr>
		学生电话:<input type="text" v-model="stu.phone">
		<hr>
		<p v-if="addstu==0">学生密码:<input type="text" v-model="stu.pwd"></p>
		<hr>
		
		图片:<input type="file" ref = "img">
		<p>
			班级:<select v-model="stu.clz.clzno">
				<option v-for="(x,idx) in clzs" v-bind:text="x.clzname" v-bind:value="x.clzno">
					{{x.clzname}}
				</option>
			</select>
		</p>
		<button @click="add()" v-if="addstu==0">添加</button>
		<button @click="update()" v-if="addstu>0">修改</button>
	</div>
</template>

<script setup>
	
</script>

<script>
	export default{
		data(){
			return{
				stu:{uid:0,clz:{clzno:''}},
				clzs:[],
				stuj:{uid:0,clz:{clzno:''}},
				token:'',
				addstu:-1,  //-1 显示  0 添加  
			}
		},
		methods:{
			go2Add(){
				this.addstu = 0;
				let stu2 = JSON.stringify(this.stuj);
				this.stu = JSON.parse(stu2);
			},
			add(){
				let fd = new FormData();
				fd.append("uid",this.stu.uid);
				fd.append("uname",this.stu.uname);
				fd.append("phone",this.stu.phone);
				fd.append("pwd",this.stu.pwd);
				fd.append("clz.clzno",this.stu.clz.clzno);
				var  filex = this.$refs.img.files[0];  //获取文件上传第一个文件内容
				fd.append("mypic",filex);
				
				this.$post("/api/adm/stu/add",fd)
				.then((resp)=>{
					console.log(resp.data);
					
				})
			},
			
			update(){
				let fd = new FormData();
				fd.append("uid",this.stu.uid);
				fd.append("uname",this.stu.uname);
				fd.append("phone",this.stu.phone);
				fd.append("pwd",this.stu.pwd);
				fd.append("clz.clzno",this.stu.clz.clzno);
				var  filex = this.$refs.img.files[0];  //获取文件上传第一个文件内容
				fd.append("mypic",filex);
				this.$post("/api/adm/stu/update",fd,)
				.then((resp)=>{
					console.log(resp.data);
					
				})
			}
			
			
		},
		
		
		mounted(){
			console.log("uid====");
			console.log(this.$route.query.uid);
			let path = "";
			path = "/"+this.$route.query.uid;
			if(this.$route.query.uid>0){
				this.addstu = this.$route.query.uid;
				this.$get("/api/adm/stu/findById"+path)
				.then((resp)=>{
					console.log(resp.data.data)
					this.stu = resp.data.data
				})
			}
			this.$get("/api/adm/clz/findAll")
			.then((resp)=>{
				console.log(resp.data.data) //班级列表
				this.clzs = resp.data.data
			})
			
		}
		
	}
</script>

<style>
</style>
